package com.zhongxiangspace.service.impl;

import com.zhongxiangspace.entity.Permissions;
import com.zhongxiangspace.entity.VO.PermissionsVO;
import com.zhongxiangspace.mapper.PermissionsMapper;
import com.zhongxiangspace.service.PermissionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 权限管理
 */
@Service
public class PermissionsServiceImpl implements PermissionsService {

    @Autowired
    private PermissionsMapper permissionsMapper;

    @Override
    public List<PermissionsVO> queryAllByID(Integer id) {
        List<PermissionsVO> permissionsVOList = new ArrayList<>();
        List<Permissions> permissionsList = this.permissionsMapper.queryAll();
        for (int i = 0; i < permissionsList.size(); i++) {
            if (permissionsList.get(i).getParent_id() == 0) {
                PermissionsVO p_i = new PermissionsVO();
                p_i.setId(permissionsList.get(i).getId());
                p_i.setName(permissionsList.get(i).getName());
                p_i.setUrl(permissionsList.get(i).getUrl());
                p_i.setParent_id(permissionsList.get(i).getParent_id());
                List<PermissionsVO> p_i_list=new ArrayList<>();
                for (int j = 0; j < permissionsList.size(); j++) {
                    if (p_i.getId() == permissionsList.get(j).getParent_id()) {
                        PermissionsVO p_j = new PermissionsVO();
                        p_j.setId(permissionsList.get(j).getId());
                        p_j.setName(permissionsList.get(j).getName());
                        p_j.setUrl(permissionsList.get(j).getUrl());
                        p_j.setParent_id(permissionsList.get(j).getParent_id());
                        List<PermissionsVO> p_y_list=new ArrayList<>();
                        for (int y = 0; y < permissionsList.size(); y++) {
                            if (p_j.getId() == permissionsList.get(y).getParent_id()) {
                                PermissionsVO p_y = new PermissionsVO();
                                p_y.setId(permissionsList.get(y).getId());
                                p_y.setName(permissionsList.get(y).getName());
                                p_y.setUrl(permissionsList.get(y).getUrl());
                                p_y.setParent_id(permissionsList.get(y).getParent_id());
                                p_y_list.add(p_y);
                            }
                        }
                        p_j.setPermissionsList(p_y_list);
                        p_i_list.add(p_j);
                    }
                }
                p_i.setPermissionsList(p_i_list);
                permissionsVOList.add(p_i);
            }
        }
        return permissionsVOList;
    }
}
